package main

import (
    "git.oschina.net/janpoem/go-agi"
)

import (
    "fmt"
    "math/rand"
    "strconv"
    "bytes"
    "strings"
)

func randStr() (string) {
    return strconv.FormatInt(rand.Int63(), 36)
}

var sp_str1 = randStr()
var sp_str2 = randStr()
var sp_str3 = randStr()
var sp_str4 = randStr()
var sp_str5 = randStr()
var sp_str6 = randStr()

func test_2_args() {
    max := 1000000
    dur1 := agi.TimesTest(max, func() {
        fmt.Sprintf("SELECT %s FROM %s", sp_str1, sp_str2)
    })

    dur2 := agi.TimesTest(max, func() {
        bf := bytes.NewBufferString("SELECT")
        bf.WriteString(" ")
        bf.WriteString(sp_str1)
        bf.WriteString(" ")
        bf.WriteString("FROM")
        bf.WriteString(sp_str2)
    })

    dur3 := agi.TimesTest(max, func() {
        bf := "SELECT " + sp_str1 + " FROM " + sp_str2
        if len(bf) > 0 {

        }
    })

    dur4 := agi.TimesTest(max, func() {
        bf := strings.Join([]string { "SELECT", sp_str1, "FROM", sp_str2 }, "")
        if len(bf) > 0 {

        }
    })

    fmt.Println(dur1.Seconds())
    fmt.Println(dur2.Seconds())
    fmt.Println(dur3.Seconds())
    fmt.Println(dur4.Seconds())
}

func test_4_args() {
    max := 1000000
    dur1 := agi.TimesTest(max, func() {
        fmt.Sprintf("SELECT %s FROM %s WHERE %s ORDER BY %s", sp_str1, sp_str2, sp_str3, sp_str4)
    })

    dur2 := agi.TimesTest(max, func() {
        bf := bytes.NewBufferString("SELECT ")
        bf.WriteString(sp_str1)
        bf.WriteString(" ")
        bf.WriteString("FROM ")
        bf.WriteString(sp_str2)
        bf.WriteString(" ")
        bf.WriteString("WHERE ")
        bf.WriteString(sp_str3)
        bf.WriteString(" ")
        bf.WriteString("ORDER BY ")
        bf.WriteString(sp_str4)
    })

    dur3 := agi.TimesTest(max, func() {
        bf := "SELECT " + sp_str1 + " FROM " + sp_str2 + " WHERE " + sp_str3 + " ORDER BY " + sp_str4
        if len(bf) > 0 {

        }
    })

    dur4 := agi.TimesTest(max, func() {
        bf := strings.Join([]string { "SELECT", sp_str1, "FROM", sp_str2, "WHERE", sp_str3, "ORDER BY", sp_str4 }, "")
        if len(bf) > 0 {

        }
    })

    fmt.Println(dur1.Seconds())
    fmt.Println(dur2.Seconds())
    fmt.Println(dur3.Seconds())
    fmt.Println(dur4.Seconds())
}

func test_6_args() {
    max := 1000000
    dur1 := agi.TimesTest(max, func() {
        fmt.Sprintf("SELECT %s FROM %s WHERE %s ORDER BY %s GROUP BY %s LIMIT %s", sp_str1, sp_str2, sp_str3, sp_str4, sp_str5, sp_str6)
    })

    dur2 := agi.TimesTest(max, func() {
        bf := bytes.NewBufferString("SELECT ")
        bf.WriteString(sp_str1)
        bf.WriteString(" ")
        bf.WriteString("FROM ")
        bf.WriteString(sp_str2)
        bf.WriteString(" ")
        bf.WriteString("WHERE ")
        bf.WriteString(sp_str3)
        bf.WriteString(" ")
        bf.WriteString("ORDER BY ")
        bf.WriteString(sp_str4)
        bf.WriteString(" ")
        bf.WriteString("GROUP BY ")
        bf.WriteString(sp_str5)
        bf.WriteString(" ")
        bf.WriteString("LIMIT ")
        bf.WriteString(sp_str6)
    })

    dur3 := agi.TimesTest(max, func() {
        bf :=   "SELECT " + sp_str1 + " FROM " + sp_str2 +
        " WHERE " + sp_str3 + " ORDER BY " + sp_str4 +
        " GROUP BY " + sp_str5 + " LIMIT " + sp_str6
        if len(bf) > 0 {

        }
    })

    dur4 := agi.TimesTest(max, func() {
        bf := strings.Join([]string {
            "SELECT", sp_str1, "FROM", sp_str2,
            "WHERE", sp_str3, "ORDER BY", sp_str4,
            "GROUP BY", sp_str5, "LIMIT", sp_str6,
        }, "")
        if len(bf) > 0 {

        }
    })

    fmt.Println(dur1.Seconds())
    fmt.Println(dur2.Seconds())
    fmt.Println(dur3.Seconds())
    fmt.Println(dur4.Seconds())
}

func main() {
    test_2_args()
//    test_4_args()
//    test_6_args()
}
